<template>

    <!-- 任务办理弹窗 -->
    <el-dialog
            v-if="taskId"
            title="任务办理"
            :visible.sync="taskDealWithoutFormDialog.visible"
    >
        <Form
                ref="dealWithoutFormForm"
                :form-items="taskDealWithoutFormDialog.formItems"
                label-width=""
        ></Form>
        <span slot="footer" class="dialog-footer">
                <el-button @click="taskDealWithoutFormDialog.visible = false">取 消</el-button>
                <el-button type="primary" @click="doTaskAssign" :loading="taskDealWithoutFormDialog.submitLoading">确 定</el-button>

            </span>
    </el-dialog>
</template>

<script>
    import Form from '../../../../common-util/src/components/page/Form.vue'
    export default {
        name: "TaskDealWithoutFormDialog",
        components:{
            Form
        },
        props:{
            taskId:{
                type: String,
            },
            onSuccess:{
                type: Function
            }
        },
        data(){
            return {
                taskDealWithoutFormDialog:{
                    visible: false,
                    // 提交按钮
                    submitLoading: false,
                    formItems:[
                        {
                            field: {
                                name: 'comment',
                                value: '同意'
                            },
                            element:{
                                type: 'textarea',
                                label: '审批意见',
                                required: true
                            }
                        },
                    ]
                }
            }
        },
        methods:{
            show(){
                this.taskDealWithoutFormDialog.visible = true
            },
            doTaskAssign(){
                this.$refs.dealWithoutFormForm.getFormRef().validate((valid) => {
                    if (valid) {
                        this.taskDealWithoutFormDialog.submitLoading = true
                        let form = this.$refs.dealWithoutFormForm.getFormData()
                        form.taskId = this.taskId
                        this.axios.post(this.$baseURL.workflow + '/workflow/task/withoutForm/completeTaskWithoutForm',
                            form).then(res =>{
                            this.taskDealWithoutFormDialog.visible = false
                            this.$message.success('任务办理成功')
                            // 重新加载数据
                            if(this.onSuccess){
                                this.onSuccess()
                            }
                        }).catch(error => {
                            if(error.response){
                                this.$message.error(error.response.data.msg)
                            }else {
                                this.$message.error('网络错误或服务不可用')
                            }
                        }).finally(()=>{
                            this.taskDealWithoutFormDialog.submitLoading = false
                        })
                    } else {
                        return false;
                    }
                });
            }
        }

    }
</script>

<style scoped>

</style>